package com.paic.mhis.hcpms.agreement.protocol.biz.validator;

import java.util.Map;

import org.springframework.stereotype.Component;

import com.paic.mhis.common.validator.HcpmsBaseValidator;
import com.paic.mhis.common.validator.MapValidator;
import com.paic.mhis.common.validator.ValidatorList;
import com.paic.mhis.hcpms.agreement.protocol.util.PreAuthorizationValidatorConstants;
import com.paic.pafa.app.biz.bo.validation.ValidStatus;

@Component("preAuthorizationValidator")
public class PreAuthorizationValidator extends HcpmsBaseValidator {

	@Override
	public Map<Object, Object> validate(Map<Object, Object> fieldMap) {
		validatorList = new ValidatorList();
		validatorList.setValidateAll(true);

		// 常规验证
		validateFieldNotNull();
		validateNumber();
		validateFieldLengthLimit();
		validateInteger();

		ValidStatus validStatus = validatorList.validate(fieldMap);

		return throwErrorMassage(validStatus);
	}

	/* 验证非空字段 */
	private void validateFieldNotNull() {
		validatorList
				.add(MapValidator
						.require(
								PreAuthorizationValidatorConstants.ITEM_TYPE_REQUIRE_VALIDATE,
								PreAuthorizationValidatorConstants.ITEM_TYPE_REQUIRE_VALIDATE_MSG));
		validatorList
				.add(MapValidator
						.require(
								PreAuthorizationValidatorConstants.ITEM_CODE_REQUIRE_VALIDATE,
								PreAuthorizationValidatorConstants.ITEM_CODE_REQUIRE_VALIDATE_MSG));
		validatorList
				.add(MapValidator
						.require(
								PreAuthorizationValidatorConstants.ITEM_NAME_REQUIRE_VALIDATE,
								PreAuthorizationValidatorConstants.ITEM_NAME_REQUIRE_VALIDATE_MSG));
	}

	/* 验证长度限制字段 */
	private void validateFieldLengthLimit() {
		validatorList
				.add(MapValidator
						.maxLength(
								PreAuthorizationValidatorConstants.ITEMNAME_CODE_FROM_REQUIRE_LENGTH,
								32,
								PreAuthorizationValidatorConstants.ITEMNAME_CODE_FROM_REQUIRE_LENGTH_MSG));
		validatorList
				.add(MapValidator
						.maxLength(
								PreAuthorizationValidatorConstants.ITEM_NAME_FROM_REQUIRE_LENGTH,
								60,
								PreAuthorizationValidatorConstants.ITEM_NAME_FROM_REQUIRE_LENGTH_MSG));
		validatorList
				.add(MapValidator
						.maxLength(
								PreAuthorizationValidatorConstants.AGE_FROM_REQUIRE_LENGTH,
								15,
								PreAuthorizationValidatorConstants.AGE_FROM_REQUIRE_LENGTH_MSG));
	}

	/* 验证数字长度 */
	private void validateNumber() {
		validatorList
				.add(MapValidator
						.number(PreAuthorizationValidatorConstants.AMOUNT_NUMBER_REQUIRE,
								12,
								2,
								PreAuthorizationValidatorConstants.AMOUNT_NUMBER_REQUIRE_MSG));
		validatorList
				.add(MapValidator
						.number(PreAuthorizationValidatorConstants.QUANTITY_NUMBER_REQUIRE,
								14,
								0,
								PreAuthorizationValidatorConstants.QUANTITY_NUMBER_REQUIRE_MSG));
	}

	/* 验证整数字段 */
	private void validateInteger() {
		validatorList
				.add(MapValidator
						.integer(
								PreAuthorizationValidatorConstants.USING_TIMES_NUMBER_REQUIRE,
								9999999999l,
								PreAuthorizationValidatorConstants.USING_TIMES_NUMBER_REQUIRE_MSG));
		validatorList
				.add(MapValidator
						.integer(
								PreAuthorizationValidatorConstants.USING_QUANTITY_NUMBER_REQUIRE,
								9999999999l,
								PreAuthorizationValidatorConstants.USING_QUANTITY_NUMBER_REQUIRE_MSG));
		validatorList
				.add(MapValidator
						.integer(
								PreAuthorizationValidatorConstants.USING_AGE_FROM_REQUIRE_LENGTH,
								9999999999l,
								PreAuthorizationValidatorConstants.USING_AGE_FROM_REQUIRE_LENGTH_MSG));
	}
}
